package com.my.algorithmlearning.leetcode;

import com.my.algorithmlearning.entity.ListNode;
import org.springframework.util.Assert;

/**
 * @ClassName Medium_19_RemoveNthFromEnd
 * @Description Medium_19_RemoveNthFromEnd
 * @Author wanghaiwei
 * @Date 2023/2/6 10:10
 */
public class Medium_19_RemoveNthFromEnd {
    public ListNode removeNthFromEndRecursive(ListNode head, int n) {
        if (head == null) {
            return null;
        }
        this.count = n + 1;
        ListNode locateNode = locateNode(head);
        if (locateNode == null) {
            head = head.next;
            return head;
        } else if (locateNode.next != null) {
            locateNode.next = locateNode.next.next;
        }
        return head;
    }

    int count;

    private ListNode locateNode(ListNode head) {
        if (head.next == null) {
            count--;
            return null;
        }
        ListNode locateNode = locateNode(head.next);
        if (locateNode != null) {
            return locateNode;
        } else if (count == 1) {
            return head;
        } else {
            count--;
            return null;
        }
    }

    public ListNode removeNthFromEndTwoPointer(ListNode head, int n) {
        if (head == null) {
            return null;
        }
        ListNode p = head, q = head.next;
        int distanceOfPq = 0;
        int size = 1;
        while (q != null) {
            size++;
            if (distanceOfPq == n) {
                p = p.next;
                q = q.next;
            } else {
                q = q.next;
                distanceOfPq++;
            }
        }
        if (size == n) {
            head = head.next;
        } else if (p.next != null) {
            p.next = p.next.next;
        }
        return head;
    }

    public static void main(String[] args) {
        Medium_19_RemoveNthFromEnd solution = new Medium_19_RemoveNthFromEnd();
        String result = solution.removeNthFromEndRecursive(ListNode.convertArrayToListNode(new int[]{1, 2, 3, 4, 5}), 2).toString();
        Assert.isTrue("1,2,3,5".equals(result), "removeNthFromEndRecursive-head = [1,2,3,4,5], n = 2计算错误：预期：[1,2,3,5]，实际：" + result);
        ListNode afterRemoveNode = solution.removeNthFromEndRecursive(ListNode.convertArrayToListNode(new int[]{1}), 1);
        Assert.isTrue(afterRemoveNode == null, "removeNthFromEndRecursive-head = [1], n = 1计算错误：预期：null，实际：" + afterRemoveNode);
        afterRemoveNode = solution.removeNthFromEndRecursive(ListNode.convertArrayToListNode(new int[]{1, 2}), 1);
        Assert.isTrue("1".equals(afterRemoveNode.toString()), "removeNthFromEndRecursive-head = [1,2], n = 1计算错误：预期：1，实际：" + afterRemoveNode);
        afterRemoveNode = solution.removeNthFromEndRecursive(ListNode.convertArrayToListNode(new int[]{1, 2}), 2);
        Assert.isTrue("2".equals(afterRemoveNode.toString()), "removeNthFromEndRecursive-head = [1,2], n = 2计算错误：预期：2，实际：" + afterRemoveNode);


        result = solution.removeNthFromEndTwoPointer(ListNode.convertArrayToListNode(new int[]{1, 2, 3, 4, 5}), 2).toString();
        Assert.isTrue("1,2,3,5".equals(result), "removeNthFromEndTwoPointer-head = [1,2,3,4,5], n = 2计算错误：预期：[1,2,3,5]，实际：" + result);
        afterRemoveNode = solution.removeNthFromEndTwoPointer(ListNode.convertArrayToListNode(new int[]{1}), 1);
        Assert.isTrue(afterRemoveNode == null, "removeNthFromEndTwoPointer-head = [1], n = 1计算错误：预期：null，实际：" + afterRemoveNode);
        afterRemoveNode = solution.removeNthFromEndTwoPointer(ListNode.convertArrayToListNode(new int[]{1, 2}), 1);
        Assert.isTrue("1".equals(afterRemoveNode.toString()), "removeNthFromEndTwoPointer-head = [1,2], n = 1计算错误：预期：1，实际：" + afterRemoveNode);
        afterRemoveNode = solution.removeNthFromEndTwoPointer(ListNode.convertArrayToListNode(new int[]{1, 2}), 2);
        Assert.isTrue("2".equals(afterRemoveNode.toString()), "removeNthFromEndTwoPointer-head = [1,2], n = 2计算错误：预期：2，实际：" + afterRemoveNode);

    }
}
